import requests
import re
import multiprocessing

class Spider:
    # 请求url
    def start_request(self, num):
        url = "http://www.doutula.com/photo/list/?page={}".format(num)
        html = requests.get(url).text
        src_list = re.findall('data-original="(.*?)"', html)
        alt_list = re.findall('alt="(.*?)"', html)
        for src, alt in zip(src_list, alt_list):
            print("正在抓取: " + src[:20] + "..." + ", " + alt)
            self.down_img(src, alt)

    # 图片下载
    def down_img(self, src, alt):
        f_name = alt + ".jpg"
        response = requests.get(src)
        with open("img\\" + f_name, "wb") as f:
            f.write(response.content)


if __name__ == '__main__':
    pool = multiprocessing.Pool()
    spider = Spider()
    # 用多进程抓取
    pool.map(spider.start_request, [i for i in range(1, 10)])